export const numberDance = (config) => {
    /*
    * 参数：
    * id
    * start
    * end
    * step
    * time (单位秒)
    * format
    * mode: step / time
    * */
    if (!config.end) throw new Error('无法读取end数据')
    if (!config.step) config.step = 4
    if (!config.time) config.time = 2
    if (!config.start) config.start = 0
    if (!config.mode) config.mode = 'time'
    if (!config.format) config.format = true

    const node = document.getElementById(config.id)
    if (!node) throw new Error('未获取到DOM')
    let n = config.start
    const item = config.end / (config.time * 100)

    const timerID = setInterval(() => {
        config.mode === 'step' ? n += config.step : n += item
        let num = Math.ceil(n) + ''
        config.format ? node.innerHTML = formatting(num) : node.innerHTML = num
        if (n >= config.end) {
            clearInterval(timerID)
            config.format ? node.innerHTML = formatting(config.end) : node.innerHTML = config.end
        }
    }, 10)
}

function formatting (num) {
    num += ''
    return num.replace(/\B(?=(\d{3})+(?!\d))/g, ',')
}
